﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using qiupeng.Public;
using System.IO;

public partial class JiGouManage_JgMain_add : System.Web.UI.Page
{
    Db List = new Db();
    BindDrowDownList list = new BindDrowDownList();
    public string LineW, QxString, QxStringSta, LineWSta;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Session["username"] == null)
        {
            this.Response.Write("<script language=javascript>alert('登陆超时！');window.parent.location = '../main_d.aspx'</script>");
            return;

        }
        string sql_down_bh = "select id,Linew+JgName as aaa  from qp_hr_JiGou order by QxString asc";

        if (!IsPostBack)
        {
            list.Bind_DropDownList_unit(ParentNodesID, sql_down_bh, "id", "aaa");
        }


        if (!Page.IsPostBack)
        {
            BindAttribute();

            if (Request.QueryString["id"] != null)
            {
                try
                {
                    ParentNodesID.SelectedValue = Request.QueryString["id"].ToString();
                }
                catch
                {
                    ParentNodesID.SelectedValue = "0";
                }
            }
        }



    }
    public void BindAttribute()
    {
        Button2.Attributes["onclick"] = "javascript:return showwait();";
        Button1.Attributes["onclick"] = "javascript:return chknull();";
    }


    protected void Button2_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            Response.Redirect("JgMain.aspx?id=" + Request.QueryString["id"] + "");
        }
        else
        {
            Response.Redirect("JgMain.aspx?id=10000");
        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string sql = "select top 1 * from qp_hr_JiGou where id='" + this.ParentNodesID.SelectedValue + "' ";
        OleDbDataReader list = this.List.GetList(sql);
        if (list.Read())
        {
            this.QxStringSta = list["QxString"].ToString();
            this.LineWSta = list["LineW"].ToString();
        }
        list.Close();
        if (this.ParentNodesID.SelectedValue == "23")
        {
            string str2 = "select  top 1 * from qp_hr_JiGou where    len(QxString)=9 order by QxString desc";
            OleDbDataReader reader2 = this.List.GetList(str2);
            if (reader2.Read())
            {
                int num = int.Parse(reader2["QxString"].ToString().Substring(reader2["QxString"].ToString().Length - 4, 3)) + 1;
                this.QxString = "aaaaa" + num + "a";
                this.LineW = "|---";
            }
            else
            {
                this.QxString = this.QxStringSta + "100a";
                this.LineW = "|---";
            }
            reader2.Close();
        }
        else
        {
            string str4 = "select  * from qp_hr_JiGou where id='" + this.ParentNodesID.SelectedValue + "'";
            OleDbDataReader reader3 = this.List.GetList(str4);
            if (reader3.Read())
            {
                if (reader3["QxString"].ToString().Length == 5)
                {
                    string str5 = "select  top 1 * from qp_hr_JiGou where  QxString like '%" + reader3["QxString"].ToString() + "%' and  len(QxString)=9 order by QxString desc";
                    OleDbDataReader reader4 = this.List.GetList(str5);
                    if (reader4.Read())
                    {
                        int num2 = int.Parse(reader4["QxString"].ToString().Substring(reader4["QxString"].ToString().Length - 4, 3)) + 1;
                        this.QxString = this.QxStringSta + num2 + "a";
                        this.LineW = "|---";
                    }
                    else
                    {
                        this.QxString = this.QxStringSta + "100a";
                        this.LineW = "|---";
                    }
                    reader4.Close();
                }
                else
                {
                    string str7 = string.Concat(new object[] { "select  top 1 * from qp_hr_JiGou where  QxString like '%", reader3["QxString"].ToString(), "%' and  len(QxString)=", reader3["QxString"].ToString().Length, "+4 order by QxString desc" });
                    OleDbDataReader reader5 = this.List.GetList(str7);
                    if (reader5.Read())
                    {
                        int num3 = int.Parse(reader5["QxString"].ToString().Substring(reader5["QxString"].ToString().Length - 4, 3)) + 1;
                        this.QxString = this.QxStringSta + num3 + "a";
                        this.LineW = "|-";
                        for (int i = 0; i < (this.LineWSta.ToString().Length + 2); i++)
                        {
                            this.LineW = this.LineW + "-";
                        }
                    }
                    else
                    {
                        string str9 = string.Concat(new object[] { "select  top 1 * from qp_hr_JiGou where  QxString like '%", reader3["QxString"].ToString().Substring(0, reader3["QxString"].ToString().Length - 4), "%' and  len(QxString)=", reader3["QxString"].ToString().Length, " order by QxString desc" });
                        if (this.List.GetList(str9).Read())
                        {
                            this.QxString = this.QxStringSta + "100a";
                            this.LineW = "|-";
                            for (int j = 0; j < (this.LineWSta.ToString().Length + 2); j++)
                            {
                                this.LineW = this.LineW + "-";
                            }
                        }
                    }
                    reader5.Close();
                }
            }
            reader3.Close();
        }
        string str10 = "insert into qp_hr_JiGou (JgName,BianZhiS,remark,ParentNodesID,LineW,QxString,paixu) values ('" + this.List.GetFormatStr(this.Name.Text) + "','" + this.BianZhiS.Text + "','" + this.List.GetFormatStr(this.remark.Text) + "','" + this.List.GetFormatStr(this.ParentNodesID.SelectedValue) + "','" + this.LineW + "','" + this.QxString + "','" + this.txtPaiXu.Text + "')";
        this.List.ExeSql(str10);
        this.List.InsertLog("新增部门[" + this.Name.Text + "]", "部门管理");
        if (this.CheckBox1.Checked)
        {
            base.Response.Write("<script language=javascript>alert('提交成功！'); window.location = 'JgMain_add.aspx?id=" + base.Request.QueryString["id"] + "'</script>");
        }
        else
        {
            base.Response.Write("<script language=javascript>alert('提交成功！'); window.parent.lform.location = '../Agenciestable.aspx'</script>");
        }

    }
}
